package com.csr.csrcheck.service.impl;


import com.csr.csrcheck.mapper.NewsMapper;
import com.csr.csrcheck.pojo.News;
import com.csr.csrcheck.service.NewsService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Resource
    private NewsMapper newsMapper;

    public List<News> getAllNews() {
        return this.newsMapper.getAllNews();
    }

    public PageResult getNewspage(int pageNum, int pageSize) {
        return PageUtils.getPageResult(pageNum, pageSize, getpageinfo(pageNum, pageSize));
    }

    public boolean addNews(News news) {
        Boolean flag = Boolean.valueOf(false);
        Integer row = this.newsMapper.addNews(news);
        if (row.intValue() > 0)
            flag = Boolean.valueOf(true);
        return flag.booleanValue();
    }

    public News getnewsbyid(Integer id) {
        News news = this.newsMapper.getnewsbyid(id);
        if (news == null)
            throw new CompanyException("");
        return news;
    }

    public int updateNews(News news) {
        int row = this.newsMapper.updateNews(news);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int deleteNews(int id) {
        int row = this.newsMapper.deleteNews(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    private PageInfo<News> getpageinfo(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<News> list = this.newsMapper.getAllNews();
        return new PageInfo(list);
    }
}
